<template>
	<view class="intergralAddress">
		<view class="intergral_address_list">
			<u-swipe-action>
				<u-swipe-action-item @click="delAddress(item)" :options="item.options" v-for="(item, index) in options4"
					:disabled="item.disabled" :key="index">
					<view class="swipe-action u-border-top"
						:class="[index === options4.length - 1 && 'u-border-bottom']" @tap.stop="chooseAddress(item)">
						<view class="swipe-action__content">
							<view class="intergral_address_top">
								<view class="intergral_address_top_left_box">
									<view class="fn-sz-222 fn-cl-333333">
										<span class="mar-right-16">{{ item.fullname }}</span>
										<span>{{ item.phone }}</span>
									</view>
								</view>
								<view class="edit_address_btn" @tap.stop="editBtn(item)">编辑</view>
							</view>
							<view class="intergral_address_bottom">
								<view class="default_type" v-if="item.status == 1">【默认】</view>
								<view class="intergral_address_details">
									{{ item.address }}<span
										v-if="item.num">{{item.num}}</span>—{{ item.province }}{{ item.city }}{{ item.county }}
								</view>
							</view>
						</view>
					</view>
				</u-swipe-action-item>
			</u-swipe-action>
		</view>
		<view class="fix_btn_box"><button class="yellow_btn"
				@tap="goLink(`/pages/integralShop/addAddress?pageType=${pageType}`)">添加地址</button></view>
	</view>
</template>

<script>
	import com from '../../mixin/index.js';

	export default {
		mixins: [com],
		data() {
			return {
				options4: [{
					phone: '',
					province: '',
					city: '',
					county: '',
					address: '',
					fullname: '',
					status: '',
					disabled: false,
					options: [{
						text: '删除',
						style: {
							backgroundColor: '#F34035'
						}
					}]
				}],
				record_id: '',
				pageType: '',
				addAddress: {}
			};
		},
		onLoad(val) {
			console.log(val, '档期那个屁娃');
			if (val.record_id) {
				this.record_id = val.record_id;
			}
			if (val.pageType) {
				this.pageType = val.pageType
			}
		},
		onShow() {
			if (this.addAddress.id) {
				this.chooseAddress(this.addAddress);
			}
			this.getMyAddress();
		},
		methods: {
			getMyAddress() {
				let optionsObj = {
					disabled: false,
					options: [{
						text: '删除',
						style: {
							backgroundColor: '#F34035'
						}
					}]
				};
				this.$api.getMyAddress().then(res => {
					console.log(res, '获取收货地址');
					if (res.code == 1) {
						this.options4 = res.data.lists.map(item => {
							return {
								...optionsObj,
								...item
							};
						});
					}
				});
			},
			delAddress(item) {
				this.$api.delAddr({
					id: item.id
				}).then(res => {
					if (res.code == 1) {
						this.$helper.toast(res.msg);
						this.options4 = this.options4.filter(elem => elem.id != item.id);
					}
				});
			},
			addAddressFunc(val) {
				this.addAddress = val;
			},
			chooseAddress(address) {
				console.log(address, '当前洗澡能缸瓦');
				let pages = getCurrentPages(); // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				if (this.record_id.length) {
					address.address_id = address.id;
					address.record_id = this.record_id;
				}
				prevPage.$vm.setAddress(address); // 給上一頁綁定方法otherFun,傳參object
				uni.navigateBack();
			},
			editBtn(item) {
				uni.navigateTo({
					url: `/pages/integralShop/addAddress?address_info=${JSON.stringify(item)}`
				});
			}
		}
	};
</script>

<style lang="scss">
	.intergralAddress {
		padding: 24rpx;
		min-height: calc(100vh);
		background-color: #f9f9f9;
		padding-bottom: 100rpx;

		.intergral_address_list {}

		/deep/.u-swipe-action {
			width: 94vw;
			border-radius: 20rpx;
		}

		/deep/.u-swipe-action-item {
			margin-bottom: 20rpx;
		}

		.swipe-action {
			width: 94%;

			&__content {
				padding: 23rpx 0rpx 23rpx 30rpx;
				background: #ffffff;
				border-radius: 20rpx;

				.intergral_address_top {
					display: flex;
					justify-content: space-between;
					align-items: center;
					width: 100%;
					margin-bottom: 16rpx;

					.intergral_address_top_left_box {}

					.edit_address_btn {
						flex: none;
						background: #efefef;
						border-radius: 25rpx;
						font-size: 24rpx;
						font-weight: 400;
						color: #666666;
						padding: 8rpx 26rpx;
					}
				}

				.intergral_address_bottom {
					display: flex;
					font-size: 28rpx;

					.default_type {
						color: #fed100;
						flex: none;
					}

					.intergral_address_details {
						color: #666666;
					}
				}
			}
		}

		.fix_btn_box {
			position: fixed;
			bottom: 20rpx;
			width: 94%;
			z-index: 99999;
		}
	}
</style>